package com.wanpinghui.supplier.business;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.malinskiy.superrecyclerview.OnMoreListener;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.business.adapter.PersonalCircleAdapter;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayout;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.common.RxEventType;
import com.wanpinghui.wphlibrary.model.business.BusinessService;
import com.wanpinghui.wphlibrary.model.business.bean.ContentImg;
import com.wanpinghui.wphlibrary.model.business.bean.DynamicBean;
import com.wanpinghui.wphlibrary.model.business.bean.DynamicPageData;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.yunshl.yunshllibrary.rxbus.RxBus;
import com.yunshl.yunshllibrary.rxbus.SubscriptionBean;
import com.yunshl.yunshllibrary.utils.DevicesUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit.callback.YRequestCallback;
import rx.functions.Action1;

/***
 * 功能描述:个人中心朋友圈
 * 作者:qiujialiu
 * 时间:2018/1/26
 ***/
@ContentView(R.layout.layout_activity_personal_circle)
public class PersonalCircleActivity extends BaseActivity {
    private static final int REQUEST_PUBLISH = 11;
    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser mLayoutTitle;
    @ViewInject(R.id.recyclerview)
    private RecyclerView recyclerView;
    @ViewInject(R.id.iv_avatar)
    private ImageView imageViewAvatar;

    private PersonalCircleAdapter mAdapter;
    private int userId;
    private String userName;
    private long lastTime;
    private boolean haveNoMore;
    private String avatar;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            userId = getIntent().getIntExtra("id",0);
            userName = getIntent().getStringExtra("name");
            avatar = getIntent().getStringExtra("avatar");
        }
        mLayoutTitle.setTitle(userName);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageViewAvatar.getLayoutParams();
        layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
        layoutParams.height = (int) (DevicesUtil.getWidth(this) * 0.66);
        imageViewAvatar.setLayoutParams(layoutParams);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setNestedScrollingEnabled(false);
        mAdapter = new PersonalCircleAdapter(this);
        if (WPHSdk.getService(OAuthService.class).getUserId() == userId) {
            mAdapter.setMyCircle(true);
        }
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void bindEvents() {
        mAdapter.setOperaListener(new PersonalCircleAdapter.OnOperaListener() {
            @Override
            public void onContentClick(DynamicBean dynamicBean, boolean onlyText) {
                if (onlyText) {
                    Intent intent = new Intent(PersonalCircleActivity.this, DynamicDetailTextActivity.class);
                    intent.putExtra("data", new Gson().toJson(dynamicBean));
                    startActivity(intent);
                }else {
                    List<ContentImg> list = dynamicBean.getContentImgs();
                    if (list != null && list.size() > 0) {
                        if (list.get(0).getType() == 1) {
                            Intent intent = new Intent(PersonalCircleActivity.this, DynamicDetailImageActivity.class);
                            intent.putExtra("data", new Gson().toJson(dynamicBean));
                            startActivity(intent);
                        }else {
                            Intent intent = new Intent(PersonalCircleActivity.this, DynamicDetailImageActivity.class);
                            intent.putExtra("data", new Gson().toJson(dynamicBean));
                            startActivity(intent);
                        }
                    }else {
                        Intent intent = new Intent(PersonalCircleActivity.this, DynamicDetailTextActivity.class);
                        intent.putExtra("data", new Gson().toJson(dynamicBean));
                        startActivity(intent);
                    }
                }
            }

            @Override
            public void onPublish() {
                startActivityForResult(new Intent(PersonalCircleActivity.this,ReleaseDynamicActivity.class), REQUEST_PUBLISH);
            }
        });

        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!recyclerView.canScrollVertically(1) && !haveNoMore) {
                    getList(lastTime);
                }
            }
        });

        RxBus.getInstance().toObserverable(SubscriptionBean.RxBusSendBean.class).subscribe(new Action1<SubscriptionBean.RxBusSendBean>() {
            @Override
            public void call(SubscriptionBean.RxBusSendBean rxBusSendBean) {
                if (rxBusSendBean.type == RxEventType.UPDATE_DYNAMIC) {
                    DynamicBean dynamicBean = (DynamicBean) rxBusSendBean.content;
                    mAdapter.changeData(dynamicBean);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {

            }
        });
    }

    @Override
    public void initData() {
        lastTime = 0;
        getList(lastTime);
        if (TextUtil.isNotEmpty(avatar)) {
            imageViewAvatar.setVisibility(View.VISIBLE);
            Glide.with(PersonalCircleActivity.this).load(avatar).error(R.mipmap.business_bg_).into(imageViewAvatar);
        }else {
            imageViewAvatar.setImageResource(R.mipmap.business_bg_);
        }
    }

    private void getList(long lastTime) {
        WPHSdk.getService(BusinessService.class)
                .getPersonDynamic(userId, lastTime, new YRequestCallback<DynamicPageData>() {
                    @Override
                    public void onSuccess(DynamicPageData var1) {
                        setPageData(var1);
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                    }
                    @Override
                    public void onException(Throwable var1) {
                    }
                });
    }

    private void setPageData(DynamicPageData var1) {
        if (lastTime == 0) {
            mAdapter.setDatas(var1.getPdList());
        }else {
            mAdapter.addDatas(var1.getPdList());
        }
        if (var1.getPdList() != null && var1.getPdList().size() > 0) {
            lastTime = var1.getPdList().get(var1.getPdList().size()-1).getCreated_at();
        }
        if (var1.getPdList() != null && var1.getPdList().size() < 20) {
            haveNoMore = true;
        }else {
           haveNoMore = false;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_PUBLISH && resultCode == RESULT_OK) {
            lastTime = 0;
            getList(lastTime);
        }
    }
}
